package com.design.working.service.impl;

import com.design.working.bo.HandleDeptJobApplicationBO;
import com.design.working.dao.AdminDAO;
import com.design.working.dao.impl.AdminDAOImpl;
import com.design.working.dto.*;
import com.design.working.po.admin.*;
import com.design.working.service.AdminService;
import com.design.working.vo.*;
import com.design.working.vo.admin.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

import static com.design.working.enums.OperationEnum.*;


/**
 * @author gavin
 * @date 2022/5/18 10:23
 */
public class AdminServiceImpl implements AdminService {

    /**
     * 成功操作, 1
     */
    private static final int SUCCESS = 1;


    private static final int FREEDOM = 3;

    /**
     * 秋季学期, 1
     */
    private static final String AUTUMN_TERM = "1";

    /**
     * 未处理, 0
     */
    private static final int NOT_HANDLED = 0;

    /**
     * 批准, 1
     */
    private static final int APPROVED = 1;

    /**
     * 拒绝（负数表示）, -1
     */
    private static final int REJECT_IN_MINUS = -1;

    /**
     * 拒绝（0表示）, 0
     */
    private static final int REJECT_IN_ZERO = 0;

    /**
     * 离岗, 0
     */
    private static final int LEAVE_WORK = 0;

    /**
     * 离岗, -1
     */
    private static final int LEAVE_WORK_MINUS = -1;

    /**
     * 换岗, 1
     */
    private static final int CHANGE_WORK = 1;

    /**
     * 在岗, 1
     */
    private static final int WORKING = 1;

    /**
     * 结岗, 0
     */
    private static final int WORKING_DONE = 0;

    /**
     * 缺勤, 0
     */
    private static final int NOT_ATTEND = 0;

    /**
     * 出勤, 1
     */
    private static final int ATTEND = 1;

    /**
     * 请假, 2
     */
    private static final int LEAVE = 2;

    /**
     * 男，M
     */
    private static final String MALE = "M";

    /**
     * 女，F
     */
    private static final String FEMALE = "F";

    /**
     * 未发放, 0
     */
    private static final int NOT_PAY = 0;

    /**
     * 已发放, 1
     */
    private static final int PAID = 1;

    /**
     * 删除岗位, 0
     */
    private static final int DELETE_WORK = 0;

    /**
     * 一月, 1
     */
    private static final int JANUARY = 1;

    /**
     * 十二月, 12
     */
    private static final int DECEMBER = 12;

    /**
     * 无审核员, "无审核员"
     */
    private static final String NO_EDITOR = "无审核员";

    /**
     * 无意见, "无意见"
     */
    private static final String NO_TEXT = "无意见";

    /**
     * 未处理, "未处理"
     */
    private static final String NO_HANDLE_STRING = "未处理";

    /**
     * 一个月的第一天, "-01"
     */
    private static final String FIRST_DAY_OF_MONTH = "-01";

    private final AdminDAO adminDAO = new AdminDAOImpl();

    /**
     * 查找未处理的学生工作申请记录
     *
     * @param pageControlDTO 分页参数
     * @return 未处理学生的工作申请记录
     */
    @Override
    public ResponseVO<List<AdminHandleStudentWorkApplicationVO>> selectStudentWorkApplication(PageControlDTO pageControlDTO) {

        int size = adminDAO.selectStudentWorkApplication(NO_PAGE.getVal(), pageControlDTO.getLimit()).size();
        // 如果没有需要审核的学生申请
        if (size == 0) {
            return ResponseVO.error("暂时没有要处理的学生申请！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();
        List<AdminHandleStudentWorkApplicationVO> list = adminDAO.selectStudentWorkApplication(page, pageControlDTO.getLimit());
        return ResponseVO.success(size, list);
    }

    /**
     * 插入管理员对学生申请记录的审批内容
     *
     * @param uid                              管理员uid
     * @param adminHandleStudentApplicationDTO 管理员对学生申请记录的审批内容
     * @return 成功
     */
    @Override
    public int insertStudentApplicationHanding(Integer uid, AdminHandleStudentApplicationDTO adminHandleStudentApplicationDTO) {

        int adminId = adminDAO.selectAdminId(uid);
        adminDAO.insertStudentApplicationHanding(adminId, adminHandleStudentApplicationDTO);
        return SUCCESS;
    }

    /**
     * 查找申请表中学生申请工作填写的个人信息
     *
     * @param smtId 学生申请工作表中对应的个人信息记录id
     * @return 学生申请工作填写的个人信息
     */
    @Override
    public ResponseVO<StudentApplicationDetailVO> selectStudentApplicationDetail(Integer smtId) {

        StudentApplicationDetailPO studentApplicationDetailPO = adminDAO.selectStudentApplicationDetail(smtId);

        String needyStudent = null;
        switch (studentApplicationDetailPO.getNeedyStudent()) {
            case REJECT_IN_ZERO:
                needyStudent = "否";
                break;
            case APPROVED:
                needyStudent = "是";
                break;
            default:
        }
        StudentApplicationDetailVO studentApplicationDetailVO = new StudentApplicationDetailVO();
        studentApplicationDetailVO.setNeedyStudent(needyStudent);
        studentApplicationDetailVO.setPoliticalStatus(studentApplicationDetailPO.getPoliticalStatus());
        studentApplicationDetailVO.setDorm(studentApplicationDetailPO.getDorm());
        studentApplicationDetailVO.setEmail(studentApplicationDetailPO.getEmail());
        studentApplicationDetailVO.setStrength(studentApplicationDetailPO.getStrength());
        studentApplicationDetailVO.setPhone(studentApplicationDetailPO.getPhone());
        studentApplicationDetailVO.setFreeTime(studentApplicationDetailPO.getFreeTime());
        studentApplicationDetailVO.setFreeTime(studentApplicationDetailPO.getFreeTime());

        return ResponseVO.success(studentApplicationDetailVO);
    }

    /**
     * 查找辅导员和用工部门对学生申请表的意见
     *
     * @param swaId 学生申请工作表中id
     * @return 辅导员和用工部门对学生申请表的意见
     */
    @Override
    public ResponseVO<AdminPreAdviceVO> selectStudentPreAdvice(Integer swaId) {

        AdminPreAdvicePO adminPreAdvicePO = adminDAO.selectStudentPreAdvice(swaId);
        String counselorPassed = null;
        switch (adminPreAdvicePO.getCounselorPassed()) {
            case NOT_HANDLED:
                counselorPassed = "未处理";
                break;
            case REJECT_IN_MINUS:
                counselorPassed = "拒绝";
                break;
            case APPROVED:
                counselorPassed = "批准";
                break;
            default:
        }
        String deptEmployeePassed = null;
        switch (adminPreAdvicePO.getDeptEmployeePassed()) {
            case NOT_HANDLED:
                deptEmployeePassed = "未处理";
                break;
            case REJECT_IN_MINUS:
                deptEmployeePassed = "拒绝";
                break;
            case APPROVED:
                deptEmployeePassed = "批准";
                break;
            default:
        }
        AdminPreAdviceVO adminPreAdviceVO = new AdminPreAdviceVO();
        adminPreAdviceVO.setCounselor(adminPreAdvicePO.getCounselor());
        adminPreAdviceVO.setCounselorPassed(counselorPassed);
        adminPreAdviceVO.setCounselorText(adminPreAdvicePO.getCounselorText());
        adminPreAdviceVO.setDeptEmployee(adminPreAdvicePO.getDeptEmployee());
        adminPreAdviceVO.setDeptEmployeePassed(deptEmployeePassed);
        adminPreAdviceVO.setDeptEmployeeText(adminPreAdvicePO.getDeptEmployeeText());

        return ResponseVO.success(adminPreAdviceVO);
    }

    /**
     * 查询学生岗位变动申请
     *
     * @param pageControlDTO 分页参数
     * @return 学生岗位变动申请的条数
     */
    @Override
    public ResponseVO<List<AdminHandleStudentWorkMotionVO>> selectStudentWorkMotion(PageControlDTO pageControlDTO) {
        int size = adminDAO.selectStudentWorkMotion(NO_PAGE.getVal(), pageControlDTO.getLimit()).size();
        // 如果没有需要审核的学生申请
        if (size == 0) {
            return ResponseVO.error("暂时没有要处理的学生申请！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<AdminHandleStudentWorkMotionPO> temp = adminDAO.selectStudentWorkMotion(page, pageControlDTO.getLimit());
        List<AdminHandleStudentWorkMotionVO> list = new ArrayList<>();

        // 转换数据
        for (AdminHandleStudentWorkMotionPO adminHandleStudentWorkMotionPO : temp) {

            AdminHandleStudentWorkMotionVO adminHandleStudentWorkMotionVO = new AdminHandleStudentWorkMotionVO();

            String majorClass = adminHandleStudentWorkMotionPO.getMajor() + "-" + adminHandleStudentWorkMotionPO.getClazz();

            String workType = null;
            switch (adminHandleStudentWorkMotionPO.getWorkType()) {
                case LEAVE_WORK:
                    workType = "离岗";
                    break;
                case CHANGE_WORK:
                    workType = "换岗";
                    break;
                default:
            }

            adminHandleStudentWorkMotionVO.setId(adminHandleStudentWorkMotionPO.getId());
            adminHandleStudentWorkMotionVO.setCollege(adminHandleStudentWorkMotionPO.getCollege());
            adminHandleStudentWorkMotionVO.setMajorClass(majorClass);
            adminHandleStudentWorkMotionVO.setStudentExplanation(adminHandleStudentWorkMotionPO.getStudentExplanation());
            adminHandleStudentWorkMotionVO.setWorkName(adminHandleStudentWorkMotionPO.getWorkName());
            adminHandleStudentWorkMotionVO.setWorkType(workType);
            adminHandleStudentWorkMotionVO.setUid(adminHandleStudentWorkMotionPO.getUid());
            adminHandleStudentWorkMotionVO.setName(adminHandleStudentWorkMotionPO.getName());

            list.add(adminHandleStudentWorkMotionVO);
        }
        return ResponseVO.success(size, list);
    }

    /**
     * 查找用工部门对学生岗位变更表的意见
     *
     * @param id 学生岗位变更表id
     * @return 用工部门对学生岗位变更表的意见
     */
    @Override
    public ResponseVO<AdminPreAdviceVO> selectDeptPreAdvice(Integer id) {

        AdminPreAdvicePO adminPreAdvicePO = adminDAO.selectDeptPreAdvice(id);
        String deptEmployeePassed = null;
        switch (adminPreAdvicePO.getDeptEmployeePassed()) {
            case NOT_HANDLED:
                deptEmployeePassed = "未处理";
                break;
            case REJECT_IN_MINUS:
                deptEmployeePassed = "拒绝";
                break;
            case APPROVED:
                deptEmployeePassed = "批准";
                break;
            default:
        }
        AdminPreAdviceVO adminPreAdviceVO = new AdminPreAdviceVO();
        adminPreAdviceVO.setDeptEmployee(adminPreAdvicePO.getDeptEmployee());
        adminPreAdviceVO.setDeptEmployeePassed(deptEmployeePassed);
        adminPreAdviceVO.setDeptEmployeeText(adminPreAdvicePO.getDeptEmployeeText());

        return ResponseVO.success(adminPreAdviceVO);
    }

    /**
     * 插入管理员处理的学生岗位变更表意见
     *
     * @param uid                         管理员uid
     * @param adminHandleStudentMotionDTO 前端传来的表单数据
     * @return 成功
     */
    @Override
    public int insertStudentMotionHanding(Integer uid, AdminHandleStudentMotionDTO adminHandleStudentMotionDTO) {

        int adminId = adminDAO.selectAdminId(uid);
        adminDAO.insertStudentMotionHanding(adminId, adminHandleStudentMotionDTO);
        return SUCCESS;
    }

    /**
     * 查找正在工作的学生和岗位信息
     *
     * @param pageControlDTO 分页参数
     * @return 查询到的在岗学生和岗位信息
     */
    @Override
    public ResponseVO<List<AdminStudentInWorkingVO>> selectAllWorking(PageControlDTO pageControlDTO) {

        int size = adminDAO.selectAllWorking(NO_PAGE.getVal(), pageControlDTO.getLimit()).size();

        // 如果没有学生在岗
        if (size == 0) {
            return ResponseVO.error("暂时没有在岗的学生！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<AdminStudentInWorkingPO> temp = adminDAO.selectAllWorking(page, pageControlDTO.getLimit());
        List<AdminStudentInWorkingVO> list = new ArrayList<>();

        // 转换数据
        for (AdminStudentInWorkingPO adminStudentInWorkingPO : temp) {

            AdminStudentInWorkingVO adminStudentInWorkingVO = new AdminStudentInWorkingVO();
            // 学期转换
            String year = adminStudentInWorkingPO.getYear() + "-" + (adminStudentInWorkingPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(adminStudentInWorkingPO.getTerm())) ? "秋冬学期" : "春夏学期";

            adminStudentInWorkingVO.setYear(year);
            adminStudentInWorkingVO.setTerm(term);
            adminStudentInWorkingVO.setDeptName(adminStudentInWorkingPO.getDeptName());
            adminStudentInWorkingVO.setId(adminStudentInWorkingPO.getId());
            adminStudentInWorkingVO.setName(adminStudentInWorkingPO.getName());
            adminStudentInWorkingVO.setUid(adminStudentInWorkingPO.getUid());
            adminStudentInWorkingVO.setJobName(adminStudentInWorkingPO.getJobName());

            list.add(adminStudentInWorkingVO);
        }

        return ResponseVO.success(size, list);
    }

    /**
     * 查找正在工作的学生和岗位信息（通过搜索信息）
     *
     * @param pageControlDTO 分页参数
     * @param searchDTO      搜索框的数据
     * @return 查询到的在岗学生和岗位信息
     */
    @Override
    public ResponseVO<List<AdminStudentInWorkingVO>> selectAllWorkingByName(PageControlDTO pageControlDTO, SearchDTO searchDTO) {

        int size = adminDAO.selectAllWorkingByName(NO_PAGE.getVal(), pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName()).size();

        // 如果没有学生在岗
        if (size == 0) {
            return ResponseVO.error("暂时没有在岗的学生！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<AdminStudentInWorkingPO> temp = adminDAO.selectAllWorkingByName(page, pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName());
        List<AdminStudentInWorkingVO> list = new ArrayList<>();

        // 转换数据
        for (AdminStudentInWorkingPO adminStudentInWorkingPO : temp) {

            AdminStudentInWorkingVO adminStudentInWorkingVO = new AdminStudentInWorkingVO();
            // 学期转换
            String year = adminStudentInWorkingPO.getYear() + "-" + (adminStudentInWorkingPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(adminStudentInWorkingPO.getTerm())) ? "秋冬学期" : "春夏学期";

            adminStudentInWorkingVO.setYear(year);
            adminStudentInWorkingVO.setTerm(term);
            adminStudentInWorkingVO.setDeptName(adminStudentInWorkingPO.getDeptName());
            adminStudentInWorkingVO.setId(adminStudentInWorkingPO.getId());
            adminStudentInWorkingVO.setName(adminStudentInWorkingPO.getName());
            adminStudentInWorkingVO.setUid(adminStudentInWorkingPO.getUid());
            adminStudentInWorkingVO.setJobName(adminStudentInWorkingPO.getJobName());

            list.add(adminStudentInWorkingVO);
        }

        return ResponseVO.success(size, list);
    }

    /**
     * （在岗学生管理）变更岗位 - 强制将在岗学生下岗
     *
     * @param id      student_to_work的id
     * @param adminId 管理员uid
     * @return 成功
     */
    @Override
    public int forceResignStudent(Integer id, Integer adminId) {
        int newAdminId = adminDAO.selectAdminId(adminId);
        adminDAO.forceResignStudent(id, newAdminId);
        return SUCCESS;
    }

    /**
     * 查询学生岗位分配记录
     *
     * @param pageControlDTO 分页参数
     * @param searchDTO      搜索框的数据
     * @return 岗位分配记录
     */
    @Override
    public ResponseVO<List<StudentWorkDistributionRecordVO>> selectWorkDistributionRecord(PageControlDTO pageControlDTO, SearchDTO searchDTO) {

        int size = adminDAO.selectWorkDistributionRecord(NO_PAGE.getVal(), pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName()).size();

        // 如果没有学生在岗
        if (size == 0) {
            return ResponseVO.error("暂时没有分配记录！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<StudentWorkDistributionRecordPO> temp = adminDAO.selectWorkDistributionRecord(page, pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName());
        List<StudentWorkDistributionRecordVO> list = new ArrayList<>();

        // 转换数据
        for (StudentWorkDistributionRecordPO studentWorkDistributionRecordPO : temp) {

            StudentWorkDistributionRecordVO studentWorkDistributionRecordVO = new StudentWorkDistributionRecordVO();
            String year = studentWorkDistributionRecordPO.getYear() + "-" + (studentWorkDistributionRecordPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(studentWorkDistributionRecordPO.getTerm())) ? "秋冬学期" : "春夏学期";

            String type = null;
            switch (studentWorkDistributionRecordPO.getWorkType()) {
                case WORKING:
                    type = "在岗";
                    break;
                case WORKING_DONE:
                    type = "结岗";
                    break;
                case LEAVE_WORK_MINUS:
                    type = "离岗";
                    break;
                default:
            }

            studentWorkDistributionRecordVO.setDept(studentWorkDistributionRecordPO.getDept());
            studentWorkDistributionRecordVO.setWorkName(studentWorkDistributionRecordPO.getWorkName());
            studentWorkDistributionRecordVO.setWorkType(type);
            studentWorkDistributionRecordVO.setName(studentWorkDistributionRecordPO.getName());
            studentWorkDistributionRecordVO.setTerm(term);
            studentWorkDistributionRecordVO.setYear(year);
            studentWorkDistributionRecordVO.setUid(studentWorkDistributionRecordPO.getUid());

            list.add(studentWorkDistributionRecordVO);
        }

        return ResponseVO.success(size, list);
    }

    /**
     * 查询在岗学生考勤记录
     *
     * @param pageControlDTO 分页参数
     * @param searchDTO      搜索框的数据
     * @return 学生考勤记录
     */
    @Override
    public ResponseVO<List<AdminStudentAttendanceRecordVO>> selectStudentAttendance(PageControlDTO pageControlDTO, SearchDTO searchDTO) {

        Integer type = WORKING;

        int size;

        // 如果不带时间
        if ("null".equals(searchDTO.getBeginDate()) && "null".equals(searchDTO.getEndDate())) {
            size = adminDAO.selectStudentAttendance(NO_PAGE.getVal(), pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName(), type).size();
        } else {
            size = adminDAO.selectStudentAttendanceByTime(NO_PAGE.getVal(), pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName(), searchDTO.getBeginDate(), searchDTO.getEndDate(), type).size();

        }

        // 如果没有考勤记录
        if (size == 0) {
            return ResponseVO.error("暂时没有考勤记录！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<AdminStudentAttendanceRecordPO> temp;
        if (searchDTO.getBeginDate() == null && searchDTO.getEndDate() == null) {
            // 如果不走时间查询
            temp = adminDAO.selectStudentAttendance(page, pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName(), type);
        } else {
            // 走时间查询
            temp = adminDAO.selectStudentAttendanceByTime(page, pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName(), searchDTO.getBeginDate(), searchDTO.getEndDate(), type);
        }

        List<AdminStudentAttendanceRecordVO> list = new ArrayList<>();

        // 数据转换
        for (AdminStudentAttendanceRecordPO adminStudentAttendanceRecordPO : temp) {

            AdminStudentAttendanceRecordVO adminStudentAttendanceRecordVO = new AdminStudentAttendanceRecordVO();
            String year = adminStudentAttendanceRecordPO.getYear() + "-" + (adminStudentAttendanceRecordPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(adminStudentAttendanceRecordPO.getTerm())) ? "秋冬学期" : "春夏学期";

            String workType = null;
            switch (adminStudentAttendanceRecordPO.getType()) {
                case NOT_ATTEND:
                    workType = "缺勤";
                    break;
                case ATTEND:
                    workType = "出勤";
                    break;
                case LEAVE:
                    workType = "请假";
                    break;
                default:
            }

            adminStudentAttendanceRecordVO.setDate(adminStudentAttendanceRecordPO.getDate());
            adminStudentAttendanceRecordVO.setDept(adminStudentAttendanceRecordPO.getDept());
            adminStudentAttendanceRecordVO.setName(adminStudentAttendanceRecordPO.getName());
            adminStudentAttendanceRecordVO.setUid(adminStudentAttendanceRecordPO.getUid());
            adminStudentAttendanceRecordVO.setTerm(term);
            adminStudentAttendanceRecordVO.setYear(year);
            adminStudentAttendanceRecordVO.setWorkName(adminStudentAttendanceRecordPO.getWorkName());
            adminStudentAttendanceRecordVO.setEmployer(adminStudentAttendanceRecordPO.getEmployer());
            adminStudentAttendanceRecordVO.setType(workType);

            list.add(adminStudentAttendanceRecordVO);
        }

        return ResponseVO.success(size, list);
    }

    /**
     * 查询全部学生考勤记录
     *
     * @param pageControlDTO 分页参数
     * @param searchDTO      搜索框的数据
     * @return 学生考勤记录
     */
    @Override
    public ResponseVO<List<AdminStudentAttendanceRecordVO>> selectAllAttendance(PageControlDTO pageControlDTO, SearchDTO searchDTO) {

        Integer type = FREEDOM;

        int size;
        // 如果不走时间查询
        if (searchDTO.getBeginDate() == null && searchDTO.getEndDate() == null) {
            size = adminDAO.selectStudentAttendance(NO_PAGE.getVal(), pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName(), type).size();
        } else {
            size = adminDAO.selectStudentAttendanceByTime(NO_PAGE.getVal(), pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName(), searchDTO.getBeginDate(), searchDTO.getEndDate(), type).size();

        }
        // 如果没有考勤记录
        if (size == 0) {
            return ResponseVO.error("暂时没有考勤记录！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<AdminStudentAttendanceRecordPO> temp;
        // 如果不走时间查询
        if (searchDTO.getBeginDate() == null && searchDTO.getEndDate() == null) {
            temp = adminDAO.selectStudentAttendance(page, pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName(), type);
        } else {
            temp = adminDAO.selectStudentAttendanceByTime(page, pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName(), searchDTO.getBeginDate(), searchDTO.getEndDate(), type);

        }
        List<AdminStudentAttendanceRecordVO> list = new ArrayList<>();

        // 数据转换
        for (AdminStudentAttendanceRecordPO adminStudentAttendanceRecordPO : temp) {

            AdminStudentAttendanceRecordVO adminStudentAttendanceRecordVO = new AdminStudentAttendanceRecordVO();
            String year = adminStudentAttendanceRecordPO.getYear() + "-" + (adminStudentAttendanceRecordPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(adminStudentAttendanceRecordPO.getTerm())) ? "秋冬学期" : "春夏学期";

            String workType = null;
            switch (adminStudentAttendanceRecordPO.getType()) {
                case NOT_ATTEND:
                    workType = "缺勤";
                    break;
                case ATTEND:
                    workType = "出勤";
                    break;
                case LEAVE:
                    workType = "请假";
                    break;
                default:
            }

            adminStudentAttendanceRecordVO.setDate(adminStudentAttendanceRecordPO.getDate());
            adminStudentAttendanceRecordVO.setDept(adminStudentAttendanceRecordPO.getDept());
            adminStudentAttendanceRecordVO.setName(adminStudentAttendanceRecordPO.getName());
            adminStudentAttendanceRecordVO.setUid(adminStudentAttendanceRecordPO.getUid());
            adminStudentAttendanceRecordVO.setTerm(term);
            adminStudentAttendanceRecordVO.setYear(year);
            adminStudentAttendanceRecordVO.setWorkName(adminStudentAttendanceRecordPO.getWorkName());
            adminStudentAttendanceRecordVO.setEmployer(adminStudentAttendanceRecordPO.getEmployer());
            adminStudentAttendanceRecordVO.setType(workType);

            list.add(adminStudentAttendanceRecordVO);
        }

        return ResponseVO.success(size, list);
    }

    /**
     * 查询学生相关信息
     *
     * @param pageControlDTO 分页参数
     * @param searchDTO      搜索框的数据
     * @return 学生相关信息
     */
    @Override
    public ResponseVO<List<StudentCommonVO>> selectStudent(PageControlDTO pageControlDTO, SearchDTO searchDTO) {

        int size = adminDAO.selectStudent(NO_PAGE.getVal(), pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName()).size();

        // 如果没有对应的学生
        if (size == 0) {
            return ResponseVO.error("暂时没有学生记录！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<StudentCommonVO> list = adminDAO.selectStudent(page, pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName());
        return ResponseVO.success(size, list);
    }

    /**
     * 查询学生详细信息
     *
     * @param uid 学生学号
     * @return 学生个人详细信息
     */
    @Override
    public ResponseVO<StudentMsgVO> selectStudentMsg(Integer uid) {

        StudentMsgPO studentMsgPO = adminDAO.selectStudentMsg(uid);

        // 数据转换
        String gender = null;
        switch (studentMsgPO.getGender()) {
            case MALE:
                gender = "男";
                break;
            case FEMALE:
                gender = "女";
                break;
            default:
        }
        String needyStudent = null;
        switch (studentMsgPO.getNeedStudent()) {
            case REJECT_IN_ZERO:
                needyStudent = "否";
                break;
            case APPROVED:
                needyStudent = "是";
                break;
            default:
        }

        StudentMsgVO studentMsgVO = new StudentMsgVO();

        studentMsgVO.setNeedStudent(needyStudent);
        studentMsgVO.setDorm(studentMsgPO.getDorm());
        studentMsgVO.setEmail(studentMsgPO.getEmail());
        studentMsgVO.setGender(gender);
        studentMsgVO.setStrength(studentMsgPO.getStrength());
        studentMsgVO.setPhone(studentMsgPO.getPhone());
        studentMsgVO.setPoliticalStatus(studentMsgPO.getPoliticalStatus());

        return ResponseVO.success(studentMsgVO);
    }

    /**
     * 查询学生申请岗位的信息
     *
     * @param pageControlDTO 分页参数
     * @return 学生申请岗位的信息
     */
    @Override
    public ResponseVO<List<StudentWorkApplicationAllVO>> selectStudentApplicationRecord(PageControlDTO pageControlDTO, SearchDTO searchDTO) {

        int size = adminDAO.selectStudentApplicationRecord(NO_PAGE.getVal(), pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName()).size();
        // 如果没有对应的学生
        if (size == 0) {
            return ResponseVO.error("暂时没有申请记录！");
        }
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();

        List<StudentWorkApplicationAllPO> temp = adminDAO.selectStudentApplicationRecord(page, pageControlDTO.getLimit(), searchDTO.getId(), searchDTO.getName());
        List<StudentWorkApplicationAllVO> list = new ArrayList<>();

        // 数据转换
        for (StudentWorkApplicationAllPO studentWorkApplicationAllPO : temp) {

            StudentWorkApplicationAllVO studentWorkApplicationAllVO = new StudentWorkApplicationAllVO();
            String year = studentWorkApplicationAllPO.getYear() + "-" + (studentWorkApplicationAllPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(studentWorkApplicationAllPO.getTerm())) ? "秋冬学期" : "春夏学期";

            String type = null;
            switch (studentWorkApplicationAllPO.getType()) {
                case NOT_HANDLED:
                    type = "未处理";
                    break;
                case APPROVED:
                    type = "批准";
                    break;
                case REJECT_IN_MINUS:
                    type = "拒绝";
                    break;
                default:
            }

            studentWorkApplicationAllVO.setDept(studentWorkApplicationAllPO.getDept());
            studentWorkApplicationAllVO.setName(studentWorkApplicationAllPO.getName());
            studentWorkApplicationAllVO.setWorkName(studentWorkApplicationAllPO.getWorkName());
            studentWorkApplicationAllVO.setId(studentWorkApplicationAllPO.getId());
            studentWorkApplicationAllVO.setTerm(term);
            studentWorkApplicationAllVO.setYear(year);
            studentWorkApplicationAllVO.setWorkDetail(studentWorkApplicationAllPO.getWorkDetail());
            studentWorkApplicationAllVO.setUid(studentWorkApplicationAllPO.getUid());
            studentWorkApplicationAllVO.setSmtId(studentWorkApplicationAllPO.getSmtId());
            studentWorkApplicationAllVO.setType(type);

            list.add(studentWorkApplicationAllVO);
        }
        return ResponseVO.success(size, list);
    }

    /**
     * 查询学生岗位申请的记录
     *
     * @param swaId 学生岗位申请表id
     * @return 学生岗位申请审批记录
     */
    @Override
    public ResponseVO<StudentApplicationExaminationVO> selectStudentApplicationExamination(Integer swaId) {

        StudentApplicationExaminationVO studentApplicationExaminationVO = new StudentApplicationExaminationVO();
        // 1. 先看is_passed，判断是否审批

        // if未审批的，手动添加返回未处理
        int counselorType = adminDAO.isStudentApplicationExaminedByCounselor(swaId);
        // 辅导员未审批
        if (counselorType == NOT_HANDLED) {
            studentApplicationExaminationVO.setAdmin(NO_EDITOR);
            studentApplicationExaminationVO.setAdminPassed(NO_HANDLE_STRING);
            studentApplicationExaminationVO.setAdminText(NO_TEXT);
            studentApplicationExaminationVO.setCounselor(NO_EDITOR);
            studentApplicationExaminationVO.setCounselorPassed(NO_HANDLE_STRING);
            studentApplicationExaminationVO.setCounselorText(NO_TEXT);
            studentApplicationExaminationVO.setDeptEmployee(NO_EDITOR);
            studentApplicationExaminationVO.setDeptEmployeePassed(NO_HANDLE_STRING);
            studentApplicationExaminationVO.setDeptEmployeeText(NO_TEXT);

            return ResponseVO.success(studentApplicationExaminationVO);
        }

        // 部门未审批
        int deptType = adminDAO.isStudentApplicationExaminedByDeptEmployee(swaId);
        if (deptType == NOT_HANDLED) {
            StudentApplicationExaminationPO studentApplicationExaminationPO = adminDAO.selectStudentApplicationExaminationByCounselor(swaId);
            studentApplicationExaminationVO.setAdmin(NO_EDITOR);
            studentApplicationExaminationVO.setAdminPassed(NO_HANDLE_STRING);
            studentApplicationExaminationVO.setAdminText(NO_TEXT);
            studentApplicationExaminationVO.setCounselor(studentApplicationExaminationPO.getCounselor());

            String counselorPassed = null;
            switch (studentApplicationExaminationPO.getCounselorPassed()) {
                case REJECT_IN_MINUS:
                    counselorPassed = "拒绝";
                    break;
                case APPROVED:
                    counselorPassed = "批准";
                    break;
                default:
            }
            studentApplicationExaminationVO.setCounselorPassed(counselorPassed);
            studentApplicationExaminationVO.setCounselorText(studentApplicationExaminationPO.getCounselorText());
            studentApplicationExaminationVO.setDeptEmployee(NO_EDITOR);
            studentApplicationExaminationVO.setDeptEmployeePassed(NO_HANDLE_STRING);
            studentApplicationExaminationVO.setDeptEmployeeText(NO_TEXT);

            return ResponseVO.success(studentApplicationExaminationVO);
        }

        // 管理员未审批
        int adminType = adminDAO.isStudentApplicationExaminedByAdmin(swaId);
        if (adminType == NOT_HANDLED) {

            StudentApplicationExaminationPO studentApplicationExaminationPO = adminDAO.selectStudentApplicationExaminationByDeptEmployee(swaId);
            studentApplicationExaminationVO.setAdmin(NO_EDITOR);
            studentApplicationExaminationVO.setAdminPassed(NO_HANDLE_STRING);
            studentApplicationExaminationVO.setAdminText(NO_TEXT);
            String counselorPassed = null;
            switch (studentApplicationExaminationPO.getCounselorPassed()) {
                case REJECT_IN_MINUS:
                    counselorPassed = "拒绝";
                    break;
                case APPROVED:
                    counselorPassed = "批准";
                    break;
                default:
            }
            String deptEmployeePassed = null;
            switch (studentApplicationExaminationPO.getDeptEmployeePassed()) {
                case REJECT_IN_MINUS:
                    deptEmployeePassed = "拒绝";
                    break;
                case APPROVED:
                    deptEmployeePassed = "批准";
                    break;
                default:
            }
            studentApplicationExaminationVO.setCounselor(studentApplicationExaminationPO.getCounselor());
            studentApplicationExaminationVO.setCounselorPassed(counselorPassed);
            studentApplicationExaminationVO.setCounselorText(studentApplicationExaminationPO.getCounselorText());
            studentApplicationExaminationVO.setDeptEmployee(studentApplicationExaminationPO.getDeptEmployee());
            studentApplicationExaminationVO.setDeptEmployeePassed(deptEmployeePassed);
            studentApplicationExaminationVO.setDeptEmployeeText(studentApplicationExaminationPO.getDeptEmployeeText());
            studentApplicationExaminationVO.setAdmin(NO_EDITOR);
            studentApplicationExaminationVO.setAdminPassed(NO_HANDLE_STRING);
            studentApplicationExaminationVO.setAdminText(NO_TEXT);

            return ResponseVO.success(studentApplicationExaminationVO);
        }
        // 全都审批了
        StudentApplicationExaminationPO studentApplicationExaminationPO = adminDAO.selectStudentApplicationExamination(swaId);
        String counselorPassed = null;
        switch (studentApplicationExaminationPO.getCounselorPassed()) {
            case REJECT_IN_MINUS:
                counselorPassed = "拒绝";
                break;
            case APPROVED:
                counselorPassed = "批准";
                break;
            default:
        }
        String deptEmployeePassed = null;
        switch (studentApplicationExaminationPO.getDeptEmployeePassed()) {
            case REJECT_IN_MINUS:
                deptEmployeePassed = "拒绝";
                break;
            case APPROVED:
                deptEmployeePassed = "批准";
                break;
            default:
        }
        String adminPassed = null;
        switch (studentApplicationExaminationPO.getAdminPassed()) {
            case REJECT_IN_MINUS:
                adminPassed = "拒绝";
                break;
            case APPROVED:
                adminPassed = "批准";
                break;
            default:
        }
        studentApplicationExaminationVO.setAdmin(studentApplicationExaminationPO.getAdmin());
        studentApplicationExaminationVO.setAdminPassed(adminPassed);
        studentApplicationExaminationVO.setAdminText(studentApplicationExaminationPO.getAdminText());
        studentApplicationExaminationVO.setCounselor(studentApplicationExaminationPO.getCounselor());
        studentApplicationExaminationVO.setCounselorPassed(counselorPassed);
        studentApplicationExaminationVO.setCounselorText(studentApplicationExaminationPO.getCounselorText());
        studentApplicationExaminationVO.setDeptEmployee(studentApplicationExaminationPO.getDeptEmployee());
        studentApplicationExaminationVO.setDeptEmployeePassed(deptEmployeePassed);
        studentApplicationExaminationVO.setDeptEmployeeText(studentApplicationExaminationPO.getDeptEmployeeText());

        return ResponseVO.success(studentApplicationExaminationVO);
    }

    /**
     * 查询岗位工资
     *
     * @param pageControlDTO 分页信息
     * @return 岗位工资信息
     */
    @Override
    public ResponseVO<List<JobSalaryVO>> selectJobSalary(PageControlDTO pageControlDTO) {

        int size = adminDAO.selectJobSalary(NO_PAGE.getVal(), pageControlDTO.getLimit()).size();

        // 如果没有对应的工资
        if (size == 0) {
            return ResponseVO.error("暂时没有工资记录！");
        }

        // 访问数据库
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();
        List<SelectJobSalaryPO> temp = adminDAO.selectJobSalary(page, pageControlDTO.getLimit());

        // 接收结果
        List<JobSalaryVO> list = new ArrayList<>();

        // 转换数据
        for (SelectJobSalaryPO selectJobSalaryPO : temp) {
            JobSalaryVO jobSalaryVO = new JobSalaryVO();

            jobSalaryVO.setId(selectJobSalaryPO.getId());
            String year = selectJobSalaryPO.getYear() + "-" + (selectJobSalaryPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(selectJobSalaryPO.getTerm())) ? "秋冬学期" : "春夏学期";
            jobSalaryVO.setYear(year);
            jobSalaryVO.setTerm(term);
            jobSalaryVO.setDept(selectJobSalaryPO.getDept());
            jobSalaryVO.setSalary(selectJobSalaryPO.getSalary());
            jobSalaryVO.setJob(selectJobSalaryPO.getJob());

            list.add(jobSalaryVO);
        }

        return ResponseVO.success(size, list);
    }

    /**
     * 修改岗位工资
     *
     * @param updateJobSalaryDTO 岗位id和新工资
     */
    @Override
    public int updateJobSalaryById(UpdateJobSalaryDTO updateJobSalaryDTO) {
        adminDAO.updateJobSalaryById(updateJobSalaryDTO.getId(), updateJobSalaryDTO.getNewSalary());
        return SUCCESS;
    }

    /**
     * 查询学生工资
     *
     * @param pageControlDTO 分页信息
     * @return 学生工资信息
     */
    @Override
    public ResponseVO<List<StudentSalarySelectionVO>> selectStudentSalary(PageControlDTO pageControlDTO) {

        int size = adminDAO.selectStudentSalary(NO_PAGE.getVal(), pageControlDTO.getLimit()).size();

        // 如果没有对应的工资
        if (size == 0) {
            return ResponseVO.error("暂时没有工资记录！");
        }

        // 访问数据库
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();
        List<SelectStudentSalaryPO> temp = adminDAO.selectStudentSalary(page, pageControlDTO.getLimit());

        List<StudentSalarySelectionVO> list = new ArrayList<>();
        // 转换数据
        for (SelectStudentSalaryPO selectStudentSalaryPO : temp) {

            StudentSalarySelectionVO studentSalarySelectionVO = new StudentSalarySelectionVO();

            studentSalarySelectionVO.setUid(selectStudentSalaryPO.getUid());
            studentSalarySelectionVO.setName(selectStudentSalaryPO.getName());

            String year = selectStudentSalaryPO.getYear() + "-" + (selectStudentSalaryPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(selectStudentSalaryPO.getTerm())) ? "秋冬学期" : "春夏学期";
            studentSalarySelectionVO.setYear(year);
            studentSalarySelectionVO.setTerm(term);

            studentSalarySelectionVO.setDept(selectStudentSalaryPO.getDept());
            studentSalarySelectionVO.setJob(selectStudentSalaryPO.getJob());

            String workMonth;

            // 月份转换
            if (selectStudentSalaryPO.getWorkMonth().getMonthValue() != JANUARY) {
                workMonth = selectStudentSalaryPO.getWorkMonth().getYear() + "-" + (selectStudentSalaryPO.getWorkMonth().getMonthValue() - 1);
            } else {
                workMonth = (selectStudentSalaryPO.getWorkMonth().getYear() - 1) + "-" + DECEMBER;
            }
            studentSalarySelectionVO.setWorkMonth(workMonth);
            studentSalarySelectionVO.setSalary(selectStudentSalaryPO.getSalary());

            String isSettled = null;
            switch (selectStudentSalaryPO.getSettled()) {
                case NOT_PAY:
                    isSettled = "未发放";
                    break;
                case PAID:
                    isSettled = "已发放";
                    break;
                default:
            }
            studentSalarySelectionVO.setSettled(isSettled);

            list.add(studentSalarySelectionVO);
        }

        return ResponseVO.success(size, list);
    }

    /**
     * 通过姓名查询学生工资
     *
     * @param pageControlDTO 分页信息
     * @param searchDTO      搜索信息
     * @return 学生工资信息
     */
    @Override
    public ResponseVO<List<StudentSalarySelectionVO>> selectStudentSalaryByName(PageControlDTO pageControlDTO, SearchDTO searchDTO) {

        // 将发工资的时间减去一个月，如此得到的是对应工作月的工资
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String beginDate = searchDTO.getBeginDate();
        String endDate = searchDTO.getEndDate();
        LocalDate beginLocalDate = LocalDate.parse(beginDate, fmt);
        LocalDate endLocalDate = LocalDate.parse(endDate, fmt);

        String newBeginDate = beginLocalDate.getYear()
                + "-" + (beginLocalDate.getMonthValue() + 1)
                + "-" + beginLocalDate.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()).getDayOfMonth();
        String newEndDate = endLocalDate.getYear()
                + "-" + (endLocalDate.getMonthValue() + 1)
                + "-" + endLocalDate.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()).getDayOfMonth();

        if (beginLocalDate.getMonthValue() == DECEMBER) {
            newBeginDate = (beginLocalDate.getYear() + 1)
                    + "-" + JANUARY
                    + "-" + beginLocalDate.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()).getDayOfMonth();
        }
        if (endLocalDate.getMonthValue() == DECEMBER) {
            newEndDate = (endLocalDate.getYear() + 1)
                    + "-" + JANUARY
                    + "-" + endLocalDate.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()).getDayOfMonth();
        }

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-M-d");
        LocalDate localBeginDate = LocalDate.parse(newBeginDate, df);
        LocalDate localEndDate = LocalDate.parse(newEndDate, df);
        DateTimeFormatter dt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String finalBeginDate = dt.format(localBeginDate);
        String finalEndDate = dt.format(localEndDate);

        searchDTO.setBeginDate(finalBeginDate);
        searchDTO.setEndDate(finalEndDate);

        int size = adminDAO.selectStudentSalaryByName(NO_PAGE.getVal(), pageControlDTO.getLimit(), searchDTO).size();

        // 如果没有对应的工资
        if (size == 0) {
            return ResponseVO.error("暂时没有工资记录！");
        }

        // 访问数据库
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();
        List<SelectStudentSalaryPO> temp = adminDAO.selectStudentSalaryByName(page, pageControlDTO.getLimit(), searchDTO);

        List<StudentSalarySelectionVO> list = new ArrayList<>();
        // 转换数据
        for (SelectStudentSalaryPO selectStudentSalaryPO : temp) {

            StudentSalarySelectionVO studentSalarySelectionVO = new StudentSalarySelectionVO();

            studentSalarySelectionVO.setUid(selectStudentSalaryPO.getUid());
            studentSalarySelectionVO.setName(selectStudentSalaryPO.getName());

            String year = selectStudentSalaryPO.getYear() + "-" + (selectStudentSalaryPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(selectStudentSalaryPO.getTerm())) ? "秋冬学期" : "春夏学期";
            studentSalarySelectionVO.setYear(year);
            studentSalarySelectionVO.setTerm(term);

            studentSalarySelectionVO.setDept(selectStudentSalaryPO.getDept());
            studentSalarySelectionVO.setJob(selectStudentSalaryPO.getJob());

            String workMonth;

            // 月份转换
            if (selectStudentSalaryPO.getWorkMonth().getMonthValue() != JANUARY) {
                workMonth = selectStudentSalaryPO.getWorkMonth().getYear() + "-" + (selectStudentSalaryPO.getWorkMonth().getMonthValue() - 1);
            } else {
                workMonth = (selectStudentSalaryPO.getWorkMonth().getYear() - 1) + "-" + DECEMBER;
            }
            studentSalarySelectionVO.setWorkMonth(workMonth);
            studentSalarySelectionVO.setSalary(selectStudentSalaryPO.getSalary());

            String isSettled = null;
            switch (selectStudentSalaryPO.getSettled()) {
                case NOT_PAY:
                    isSettled = "未发放";
                    break;
                case PAID:
                    isSettled = "已发放";
                    break;
                default:
            }
            studentSalarySelectionVO.setSettled(isSettled);

            list.add(studentSalarySelectionVO);
        }

        return ResponseVO.success(size, list);
    }


    /**
     * 查询所有待审批的部门申请岗位
     *
     * @param pageControlDTO 分页参数
     * @return 所有待审批的部门申请岗位
     */
    @Override
    public ResponseVO<List<DeptJobApplicationVO>> selectDeptJobApplication(PageControlDTO pageControlDTO) {

        int size = adminDAO.selectDeptJobApplication(NO_PAGE.getVal(), pageControlDTO.getLimit()).size();

        // 如果没有要处理的申请
        if (size == 0) {
            return ResponseVO.error("暂时没有需要处理的申请！");
        }

        // 访问数据库
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();
        List<SelectDeptJobApplicationPO> temp = adminDAO.selectDeptJobApplication(page, pageControlDTO.getLimit());

        List<DeptJobApplicationVO> list = new ArrayList<>();

        // 转换数据
        for (SelectDeptJobApplicationPO selectDeptJobApplicationPO : temp) {

            DeptJobApplicationVO deptJobApplicationVO = new DeptJobApplicationVO();

            deptJobApplicationVO.setId(selectDeptJobApplicationPO.getId());
            deptJobApplicationVO.setDeptName(selectDeptJobApplicationPO.getDeptName());
            deptJobApplicationVO.setJobName(selectDeptJobApplicationPO.getJobName());
            // 学期转换
            String year = selectDeptJobApplicationPO.getYear() + "-" + (selectDeptJobApplicationPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(selectDeptJobApplicationPO.getTerm())) ? "秋冬学期" : "春夏学期";

            deptJobApplicationVO.setYear(year);
            deptJobApplicationVO.setTerm(term);

            list.add(deptJobApplicationVO);
        }

        return ResponseVO.success(size, list);
    }

    /**
     * 查询部门申请岗位的详细信息
     *
     * @param id dept_job_application表的id
     * @return 相关的详细信息
     */
    @Override
    public ResponseVO<DeptJobApplicationDetailVO> selectDeptJobApplicationDetail(Integer id) {
        return ResponseVO.success(adminDAO.selectDeptJobApplicationDetail(id));
    }

    /**
     * 处理对部门岗位的申请
     *
     * @param handleDeptJobApplicationDTO 前端传来的数据
     */
    @Override
    public int handleDeptJobApplication(HandleDeptJobApplicationDTO handleDeptJobApplicationDTO) {

        // 将管理员uid转为其表中对应id
        Integer adminId = adminDAO.selectAdminId(handleDeptJobApplicationDTO.getUid());
        handleDeptJobApplicationDTO.setUid(adminId);

        switch (handleDeptJobApplicationDTO.getOperation()) {
            // 批准
            case APPROVED:
                HandleDeptJobApplicationBO handleDeptJobApplicationBO = new HandleDeptJobApplicationBO();
                // 获取相关的数据
                handleDeptJobApplicationBO.setSalary(handleDeptJobApplicationDTO.getSalary());

                // 获取dept_job_application的id
                int djaId = handleDeptJobApplicationDTO.getId();

                // 获取dept_job_application相关数据，用于创建岗位
                SelectTotalDeptJobApplicationPO selectTotalDeptJobApplicationPO = adminDAO.selectTotalDeptJobApplication(djaId);

                handleDeptJobApplicationBO.setDeptEmployeeId(selectTotalDeptJobApplicationPO.getDeptEmployeeId());
                handleDeptJobApplicationBO.setJobName(selectTotalDeptJobApplicationPO.getJobName());
                handleDeptJobApplicationBO.setPlace(selectTotalDeptJobApplicationPO.getPlace());
                handleDeptJobApplicationBO.setJobDetail(selectTotalDeptJobApplicationPO.getJobDetail());
                handleDeptJobApplicationBO.setNeedCount(selectTotalDeptJobApplicationPO.getNeedCount());
                handleDeptJobApplicationBO.setJobCategoryId(selectTotalDeptJobApplicationPO.getJobCategoryId());
                handleDeptJobApplicationBO.setCampusTermId(selectTotalDeptJobApplicationPO.getCampusTermId());

                handleDeptJobApplicationBO.setDeptId(adminDAO.selectDeptIdFromDeptEmployeeId(handleDeptJobApplicationBO.getDeptEmployeeId()));
                handleDeptJobApplicationBO.setAdminId(adminId);
                // 执行操作，创建岗位
                adminDAO.approveDeptJobApplication(handleDeptJobApplicationBO, handleDeptJobApplicationDTO);
                break;
            // 拒绝
            case REJECT_IN_MINUS:
                adminDAO.rejectDeptJobApplication(handleDeptJobApplicationDTO);
                break;
            default:
        }
        return SUCCESS;
    }

    /**
     * 查询所有待审批的部门岗位变更记录
     *
     * @param pageControlDTO 分页参数
     * @return 所有待审批的部门岗位变更记录
     */
    @Override
    public ResponseVO<List<SelectDeptJobMotionVO>> selectDeptJobMotion(PageControlDTO pageControlDTO) {

        int size = adminDAO.selectDeptJobMotion(NO_PAGE.getVal(), pageControlDTO.getLimit()).size();

        // 如果没有要处理的申请
        if (size == 0) {
            return ResponseVO.error("暂时没有需要处理的申请！");
        }

        // 访问数据库
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();
        List<SelectDeptJobMotionPO> temp = adminDAO.selectDeptJobMotion(page, pageControlDTO.getLimit());

        List<SelectDeptJobMotionVO> list = new ArrayList<>();

        // 转换数据
        for (SelectDeptJobMotionPO selectDeptJobMotionPO : temp) {

            SelectDeptJobMotionVO selectDeptJobMotionVO = new SelectDeptJobMotionVO();
            selectDeptJobMotionVO.setId(selectDeptJobMotionPO.getId());
            selectDeptJobMotionVO.setDept(selectDeptJobMotionPO.getDept());
            selectDeptJobMotionVO.setName(selectDeptJobMotionPO.getName());

            // 学期转换
            String year = selectDeptJobMotionPO.getYear() + "-" + (selectDeptJobMotionPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(selectDeptJobMotionPO.getTerm())) ? "秋冬学期" : "春夏学期";

            selectDeptJobMotionVO.setYear(year);
            selectDeptJobMotionVO.setTerm(term);

            String type = null;
            // 岗位变更状态转换
            if (selectDeptJobMotionPO.getType() == DELETE_WORK) {
                type = "删除岗位";
            }

            selectDeptJobMotionVO.setType(type);
            selectDeptJobMotionVO.setReason(selectDeptJobMotionPO.getReason());
            list.add(selectDeptJobMotionVO);
        }
        return ResponseVO.success(size, list);
    }

    /**
     * 处理部门信息变动
     *
     * @param adminHandleDeptJobWorkMotionDTO 需要处理的数据
     */
    @Override
    public int insertDeptJobWorkMotionHandling(AdminHandleDeptJobWorkMotionDTO adminHandleDeptJobWorkMotionDTO) {

        // 获取job的id
        int jobId = adminDAO.selectJobIdFromDeptJobMotion(adminHandleDeptJobWorkMotionDTO.getId());

        // 转换adminId
        int adminId = adminDAO.selectAdminId(adminHandleDeptJobWorkMotionDTO.getUid());
        adminHandleDeptJobWorkMotionDTO.setUid(adminId);

        // 批准--删除岗位
        if (adminHandleDeptJobWorkMotionDTO.getType() == 0) {
            adminDAO.insertDeptJobWorkMotionHandlingIfDeleteJob(adminHandleDeptJobWorkMotionDTO, jobId);
        } else {
            // 拒绝
            adminDAO.insertDeptJobWorkMotionHandlingIfReject(adminHandleDeptJobWorkMotionDTO);
        }
        return SUCCESS;
    }

    /**
     * 查询全部岗位申请记录
     *
     * @param pageControlDTO 分页参数
     * @return 全部岗位申请记录
     */
    @Override
    public ResponseVO<List<DeptJobApplicationRecordVO>> selectDeptJobApplicationRecord(PageControlDTO pageControlDTO) {

        int size = adminDAO.selectDeptJobApplicationRecord(NO_PAGE.getVal(), pageControlDTO.getLimit()).size();

        // 如果没有岗位申请记录
        if (size == 0) {
            return ResponseVO.error("暂时没有岗位申请记录！");
        }

        // 访问数据库
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();
        List<DeptJobApplicationRecordPO> temp = adminDAO.selectDeptJobApplicationRecord(page, pageControlDTO.getLimit());

        List<DeptJobApplicationRecordVO> list = new ArrayList<>();

        // 数据转换
        for (DeptJobApplicationRecordPO deptJobApplicationRecordPO : temp) {

            DeptJobApplicationRecordVO deptJobApplicationRecordVO = new DeptJobApplicationRecordVO();
            deptJobApplicationRecordVO.setId(deptJobApplicationRecordPO.getId());

            // 学期转换
            String year = deptJobApplicationRecordPO.getYear() + "-" + (deptJobApplicationRecordPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(deptJobApplicationRecordPO.getTerm())) ? "秋冬学期" : "春夏学期";

            deptJobApplicationRecordVO.setYear(year);
            deptJobApplicationRecordVO.setTerm(term);

            deptJobApplicationRecordVO.setDept(deptJobApplicationRecordPO.getDept());
            deptJobApplicationRecordVO.setJob(deptJobApplicationRecordPO.getJob());

            String type = null;
            switch (deptJobApplicationRecordPO.getType()) {
                case NOT_HANDLED:
                    type = "未处理";
                    break;
                case APPROVED:
                    type = "批准";
                    break;
                case REJECT_IN_MINUS:
                    type = "拒绝";
                    break;
                default:
            }
            deptJobApplicationRecordVO.setType(type);

            list.add(deptJobApplicationRecordVO);
        }
        return ResponseVO.success(size, list);
    }

    /**
     * 关键字查询岗位申请记录
     *
     * @param pageControlDTO              分页参数
     * @param searchDeptJobApplicationDTO 搜索的数据
     * @return 相关的岗位申请记录
     */
    @Override
    public ResponseVO<List<DeptJobApplicationRecordVO>> selectDirectDeptJobApplicationRecord(PageControlDTO pageControlDTO, SearchDeptJobApplicationDTO searchDeptJobApplicationDTO) {

        int size = adminDAO.selectDirectDeptJobApplicationRecord(NO_PAGE.getVal(), pageControlDTO.getLimit(), searchDeptJobApplicationDTO).size();

        // 如果没有岗位申请记录
        if (size == 0) {
            return ResponseVO.error("暂时没有岗位申请记录！");
        }

        // 访问数据库
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();
        List<DeptJobApplicationRecordPO> temp = adminDAO.selectDirectDeptJobApplicationRecord(page, pageControlDTO.getLimit(), searchDeptJobApplicationDTO);

        List<DeptJobApplicationRecordVO> list = new ArrayList<>();
        // 数据转换
        for (DeptJobApplicationRecordPO deptJobApplicationRecordPO : temp) {

            DeptJobApplicationRecordVO deptJobApplicationRecordVO = new DeptJobApplicationRecordVO();
            deptJobApplicationRecordVO.setId(deptJobApplicationRecordPO.getId());

            // 学期转换
            String year = deptJobApplicationRecordPO.getYear() + "-" + (deptJobApplicationRecordPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(deptJobApplicationRecordPO.getTerm())) ? "秋冬学期" : "春夏学期";

            deptJobApplicationRecordVO.setYear(year);
            deptJobApplicationRecordVO.setTerm(term);

            deptJobApplicationRecordVO.setDept(deptJobApplicationRecordPO.getDept());
            deptJobApplicationRecordVO.setJob(deptJobApplicationRecordPO.getJob());

            String type = null;
            switch (deptJobApplicationRecordPO.getType()) {
                case NOT_HANDLED:
                    type = "未处理";
                    break;
                case APPROVED:
                    type = "批准";
                    break;
                case REJECT_IN_MINUS:
                    type = "拒绝";
                    break;
                default:
            }
            deptJobApplicationRecordVO.setType(type);

            list.add(deptJobApplicationRecordVO);
        }
        return ResponseVO.success(size, list);
    }

    /**
     * 查询岗位申请记录的审批详情
     *
     * @param id dept_job_application的id
     * @return 相关审批详情
     */
    @Override
    public ResponseVO<DeptJobApplicationExaminationVO> selectDeptJobApplicationExamination(Integer id) {
        return ResponseVO.success(adminDAO.selectDeptJobApplicationExamination(id));
    }

    /**
     * 查询全部用工评价记录
     *
     * @param pageControlDTO 分页参数
     * @return 全部用工评价记录
     */
    @Override
    public ResponseVO<List<WorkEvaluationVO>> selectWorkEvaluation(PageControlDTO pageControlDTO) {

        int size = adminDAO.selectWorkEvaluation(NO_PAGE.getVal(), pageControlDTO.getLimit()).size();

        // 如果没有评价记录
        if (size == 0) {
            return ResponseVO.error("暂时没有评价记录！");
        }

        // 访问数据库
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();
        List<WorkEvaluationPO> temp = adminDAO.selectWorkEvaluation(page, pageControlDTO.getLimit());
        List<WorkEvaluationVO> list = new ArrayList<>();

        // 数据转换
        for (WorkEvaluationPO workEvaluationPO : temp) {
            WorkEvaluationVO workEvaluationVO = new WorkEvaluationVO();
            workEvaluationVO.setId(workEvaluationPO.getId());
            workEvaluationVO.setUid(workEvaluationPO.getUid());
            // 学期转换
            String year = workEvaluationPO.getYear() + "-" + (workEvaluationPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(workEvaluationPO.getTerm())) ? "秋冬学期" : "春夏学期";

            workEvaluationVO.setYear(year);
            workEvaluationVO.setTerm(term);

            workEvaluationVO.setName(workEvaluationPO.getName());
            workEvaluationVO.setJob(workEvaluationPO.getJob());
            workEvaluationVO.setDept(workEvaluationPO.getDept());

            list.add(workEvaluationVO);
        }

        return ResponseVO.success(size, list);
    }

    /**
     * 通过关键词查询部分用工评价记录
     *
     * @param pageControlDTO          分页参数
     * @param searchWorkEvaluationDTO 搜索框传入的相关参数
     * @return 部分用工评价记录
     */
    @Override
    public ResponseVO<List<WorkEvaluationVO>> selectWorkEvaluationByName(PageControlDTO pageControlDTO, SearchWorkEvaluationDTO searchWorkEvaluationDTO) {
        int size = adminDAO.selectWorkEvaluationByName(NO_PAGE.getVal(), pageControlDTO.getLimit(), searchWorkEvaluationDTO).size();

        // 如果没有评价记录
        if (size == 0) {
            return ResponseVO.error("暂时没有评价记录！");
        }

        // 访问数据库
        int page = (pageControlDTO.getPage() - 1) * pageControlDTO.getLimit();
        List<WorkEvaluationPO> temp = adminDAO.selectWorkEvaluationByName(page, pageControlDTO.getLimit(), searchWorkEvaluationDTO);
        List<WorkEvaluationVO> list = new ArrayList<>();

        // 数据转换
        for (WorkEvaluationPO workEvaluationPO : temp) {
            WorkEvaluationVO workEvaluationVO = new WorkEvaluationVO();
            workEvaluationVO.setId(workEvaluationPO.getId());
            workEvaluationVO.setUid(workEvaluationPO.getUid());
            // 学期转换
            String year = workEvaluationPO.getYear() + "-" + (workEvaluationPO.getYear() + 1);
            String term = AUTUMN_TERM.equals(String.valueOf(workEvaluationPO.getTerm())) ? "秋冬学期" : "春夏学期";

            workEvaluationVO.setYear(year);
            workEvaluationVO.setTerm(term);

            workEvaluationVO.setName(workEvaluationPO.getName());
            workEvaluationVO.setJob(workEvaluationPO.getJob());
            workEvaluationVO.setDept(workEvaluationPO.getDept());

            list.add(workEvaluationVO);
        }

        return ResponseVO.success(size, list);
    }

    /**
     * 通过id查询其对应的用工评价详细信息
     *
     * @param id dept_evaluate_student的id
     * @return 相应的用工评价详细信息
     */
    @Override
    public ResponseVO<WorkEvaluationDetailVO> selectWorkEvaluationDetailById(Integer id) {
        return ResponseVO.success(adminDAO.selectWorkEvaluationDetailById(id));
    }

    /**
     * 手动对单个学生结岗
     *
     * @param id student_to_work的id
     * @return 成功
     */
    @Override
    public int finishStudentWorkById(Integer id) {
        adminDAO.finishStudentWorkById(id);
        return SUCCESS;
    }

    /**
     * 查询某年月工资是否被统计过（实际年月的下一月，即操作月）
     * @param date 操作年月
     * @return SUCCESS - 已生成; NO - 未生成
     */
    @Override
    public int selectSalaryStatusByDate(String date) {
        return adminDAO.selectSalaryStatusByDate(date) == SUCCESS ? SUCCESS : NO.getVal();
    }

    /**
     * 统计某月工资情况(通过考勤)
     * @param date 操作年月
     * @return SUCCESS - 已统计；NO - 未统计
     */
    @Override
    public int countSalaryByDate(String date) {

        // 先查询是否统计过（sws有无统计记录）
        int flag = selectSalaryStatusByDate(date);
        if (flag == SUCCESS) {
            return NO.getVal();
        }

        // 转换发工资的年月
        String[] dateArr = date.split("-", 2);
        int year = Integer.parseInt(dateArr[0]);
        int month = Integer.parseInt(dateArr[1]);
        if (month == JANUARY) {
            month = DECEMBER;
            year -= 1;
        }
        else {
            month -= 1;
        }
        // 实际工作年月
        String trueDate;
        if (month < 10) {
            trueDate = year + "-0" + month;
        }
        else {
            trueDate = year + "-" + month;
        }

        // 工资发放时间
        String newDate = date + FIRST_DAY_OF_MONTH;

        // 统计工资情况（通过考勤）
        // 1. Map存储该月有工作记录的学生对应岗位和岗位工资（student_to_work_id）
        Map<Integer, BigDecimal> map = adminDAO.selectStudentWorkByDate(trueDate);
        // 2. 遍历Map, 同时通过student_to_work_id找date所在月的出勤情况（List存储）
        map.entrySet().parallelStream().forEach((entry) -> {
            // 该月工资
            BigDecimal salaryCount = new BigDecimal(0);
            // 岗位工资
            BigDecimal salary = entry.getValue();
            // 获取出勤情况
            List<Integer> list = adminDAO.selectWorkAttendanceById(entry.getKey(), trueDate);

            // 3. 在2中计算对应月的工资
            // 如果出勤，则增加工资
            for (int type : list) {
                if (type == ATTEND) {
                    salaryCount = salaryCount.add(salary);
                }
            }
            // 4. 插入student_work_salaries中(存入操作月)
            adminDAO.insertSalaryToStudentByDate(entry.getKey(), newDate, salaryCount);
        });

        return SUCCESS;
    }

    /**
     * 操作月发放工资
     * @param date 操作年月
     * @return SUCCESS - 已发放；NO - 未发放
     */
    @Override
    public int sendSalaryByDate(String date) {

        // 先查询是否发放
        boolean flag = isSendAndStatisticSalary(date);
        if (flag) {
            return NO.getVal();
        }
        // 补全日期
        String newDate = date + FIRST_DAY_OF_MONTH;
        // 发放
        adminDAO.updateSalaryByDate(newDate);
        return SUCCESS;
    }

    /**
     * 判断某月是否已经统计并发放过工资
     * @param date 操作年月
     * @return true - 已经统计并发放；false - 未统计并发放
     */
    private boolean isSendAndStatisticSalary(String date) {
        return adminDAO.isSendAndStatisticSalary(date) == SUCCESS;
    }
}